import { ref } from 'vue'
import router from '@/router'
// 参考:https://github.com/ag-grid/ag-grid 参数

type cellRendererParamObj = {
  action: Function
}

export type ParamsType = {
  value: string
}

/**
 *目前不兼容参数:
 *onCellValueChanged
 *
 */
export type Header = {
  headerName?: string,// 标题
  autoHeight?: boolean,
  field?: string, // 字段
  children?: Array<Header>, // 子项
  sortable?: boolean,//列是否可排列
  rowDrag?: boolean,//行是否可拖拽
  flex?: number,
  wrapText?: boolean,
  resizable?: boolean,
  wrapHeaderText?: boolean,
  autoHeaderHeight?: boolean,
  minWidth?: number,
  width?: number,
  hidden?: boolean, // 是否隐藏
  maxWidth?: number,//最大长度
  editable?: boolean, // 是否可编辑
  valueFormatter?: Function, // 值格式化
  onCellValueChanged?: Function, // 值改变触发
  cellRendererSelector?: Function,
  valueGetter?: Function,
  isOptionColumn?: boolean, // 是否是操作列
  pinned?: 'right' | 'left', // 固定列方向
  cellRenderer?: any,  // 列渲染组件名称 引用table时若需要则需要先在调用对象中引入组件
  cellRendererParams?: cellRendererParamObj, // 渲染参数 一般和cellRenderer一起使用
  onCellClicked?: Function, // 点击事件
  decoration?: Function // css修饰符
}

// 时间戳格式化
export const timestampToDate = (params: any) => {
  // 简单判断是否是时间戳
  const num = parseInt(params.value, 10)
  if (num === params.value) {
    // 是时间戳
    const timestamp = params.value * 1000 // params.value为后端linux时间戳
    const date = new Date(timestamp)
    const year = date.getFullYear()
    const month = date.getMonth() > 9 ? 1 + date.getMonth() : '0' + (1 + date.getMonth())
    const d = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
    const hours = date.getHours() > 9 ? date.getHours() : '0' + date.getHours()
    const minutes = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes()
    const seconds = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()
    return `${year}-${month}-${d} ${hours}:${minutes}:${seconds}`
  } else {
    return params.value
  }
}

/**
 * 根据路由名称加载表格
 * @returns
 */
export const table = () => {
  const template = ref('default')
  const getTable = async () => {
    // const routerName = router.currentRoute.value.name
    template.value = 'default'
  }
  return {
    template,
    getTable
  }
}
